package com.hsy.bibc.modules.login;

import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.bibc.common.Config;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.R;
import com.hsy.bibc.modules.common.TimeCount;
import com.hsy.bibc.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/29.
 */
public class ForgetPasswordActivity extends CommonBaseActivityWithSimpleActionBar {
    private EditText phone_edit, password_edit, code_edit;
    private TextView finish, code;
    private TimeCount mTimeCount;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                DPLogUtil.d(response);
                showToast(R.string.msg_find_password_success);
                CommonUtils.hideProgressDialog();
                finish();
                if (null != mTimeCount) {
                    if (mTimeCount.isRunning) {
                        mTimeCount.cancel();
                    }
                }

            }
            if (1 == what) {
                DPLogUtil.d(response);
                showToast(R.string.msg_get_code_success);
                CommonUtils.hideProgressDialog();
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View view = this.getCurrentFocus();
        if (view != null) {
            IBinder a = view.getWindowToken();
            if (a != null) {
                return imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
            }
        }


        return super.onTouchEvent(event);
    }

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        setTitle(R.string.title_forget_password);
        setTitleTextColor(R.color.tab_text_color_normal);

        code = F(R.id.code);
        phone_edit = F(R.id.phone_edit);
        password_edit = F(R.id.password_edit);
        code_edit = F(R.id.code_edit);
        finish = F(R.id.finish);

        textAPI();

        mTimeCount = TimeCount.getInstance(code, this);
        code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isEmpty(phone_edit.getText().toString())) {

                    showToast(R.string.forget_phone);
                } else {

                    CommonUtils.showProgressDialog(ForgetPasswordActivity.this);
                    mTimeCount.start();
                    Map<String, String> data = new HashMap<String, String>();
                    data.put("token", CommonUtils.getToken(ForgetPasswordActivity.this));
                    data.put("phone", phone_edit.getText().toString());
                    HttpUtil.httpPostWithParams(ForgetPasswordActivity.this, 1, Consts.API_POST_SEND_SMS, data, callback);
                }

            }
        });

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBeforePost()) {
                    CommonUtils.showProgressDialog(ForgetPasswordActivity.this);
                    Map<String, String> data = new HashMap<String, String>();
                    data.put("phone", phone_edit.getText().toString());
                    data.put("code", code_edit.getText().toString());
                    data.put("password", password_edit.getText().toString());
                    HttpUtil.httpPostWithParams(ForgetPasswordActivity.this, 0, Consts.API_POST_FORGET_PASS, data, callback);
                }
            }
        });
    }

    private void textAPI() {

        Button text_api = F(R.id.text_api);
        text_api.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(TextAPIActivity.class);
            }
        });
    }

    private boolean checkBeforePost() {
        if (isEmpty(phone_edit.getText().toString())) {
            showToast(R.string.forget_phone);
            return false;
        } else if (isEmpty(code_edit.getText().toString())) {
            showToast(R.string.forget_code);
            return false;
        } else if (isEmpty(password_edit.getText().toString())) {
            showToast(R.string.forget_password);
            return false;
        } else if (6 > password_edit.getText().toString().length()) {

            showToast(R.string.forget_password_limit);

            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mTimeCount) {
            if (mTimeCount.isRunning) {
                mTimeCount.cancel();
            }
        }
    }
}
